package com.my.system.dao.order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.my.system.dao.DataBaseConnection;
import com.my.system.object.order.Order;

public class OrderDao {
	private static Connection con = null;
	private static PreparedStatement psta = null;
	private static ResultSet rs = null;
	static {
		con = DataBaseConnection.getConnection();
	}

	public static boolean insertOrder(Order order) {
		String sql = "INSERT INTO `order`(memo,client_id,order_status_id)VALUES(?,?,?)";
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, order.getMemo());
			psta.setInt(2, order.getClient_id());

			psta.setInt(3, order.getOrder_status_id());
			con.setAutoCommit(false);
			int i = psta.executeUpdate();
			con.commit();
			con.setAutoCommit(true);
			if (i > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}

	public static boolean updateOrder(Order order) {
		String sql = "update `order` set memo=?,client_id=?,order_status_id=? where id=?";
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, order.getMemo());
			psta.setInt(2, order.getClient_id());
			psta.setInt(3, order.getOrder_status_id());
			psta.setInt(4, order.getId());
			con.setAutoCommit(false);
			int i = psta.executeUpdate();
			con.commit();
			con.setAutoCommit(true);
			if (i > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}

	public static Order searchOrderByID(int id) {
		String sql = "select * from `order` where id=?";
		Order order = null;
		try {
			psta = con.prepareStatement(sql);
			psta.setInt(1, id);
			rs = psta.executeQuery();
			if (rs.next()) {
				order = new Order();
				order.setId(rs.getInt(1));
				order.setCreate_datetime(rs.getDate(2).toString());
				order.setMemo(rs.getString(3));
				order.setClient_id(rs.getInt(4));
				order.setOrder_status_id(rs.getInt(5));
			}

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return order;
	}

	public static List<Order> searchOrderByClientID(int client_id) {
		List<Order> list = new ArrayList<Order>();
		String sql = "select * from `order` where client_id=?";
		Order order = null;
		try {
			psta = con.prepareStatement(sql);
			psta.setInt(1, client_id);
			rs = psta.executeQuery();
			while (rs.next()) {
				order = new Order();
				order.setId(rs.getInt(1));
				order.setCreate_datetime(rs.getDate(2).toString());
				order.setMemo(rs.getString(3));
				order.setClient_id(rs.getInt(4));
				order.setOrder_status_id(rs.getInt(5));
				list.add(order);
			}
			return list;

		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
}
